package com.rightsidetech.http.mq;

import com.alibaba.fastjson.JSONObject;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.utils.DateUtil;
import com.rightsidetech.http.config.WechatConfig;
import com.rightsidetech.http.wechatutil.MsgTemplate;
import com.rightsidetech.http.wechatutil.MsgTemplateParam;
import com.rightsidetech.http.wechatutil.MsgTemplatesEnum;
import com.rightsidetech.http.wechatutil.WechatMsgSendUtil;
import com.rightsidetech.trade.entity.TdCommodityOrderDetails;
import com.rightsidetech.trade.entity.TdCommodityUserOrder;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.trade.service.TdCouponUseService;
import com.rightsidetech.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MqServiceImpl implements MqService{

    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private UserInfoService userInfoService;
    @Autowired
    private WechatConfig wechatConfig;
    @Resource
    private TdCouponUseService tdCouponUseService;

    @Override
    public void updatePalceOrder(String orderNo) throws Exception {
        TdCommodityUserOrder tdCommodityUserOrder = tdCommodityUserOrderService.findByOrderNo(orderNo);
        if(null != tdCommodityUserOrder && tdCommodityUserOrder.getOrderStatus() == 0){
            ResponseData resp = tdCommodityUserOrderService.updateOrderStatus(orderNo);
            if(resp.getCodeId() == 1){
                tdCouponUseService.updateStatusByorderNo(orderNo,(byte) -1);
            }
        }
    }

    @Override
    public void sendOderMessage(String messageBody) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(messageBody);
        String typeId = jsonObject.getString("typeId");
        String orderNo = jsonObject.getString("orderNo");
        if (StringUtils.isEmpty(typeId)){
            log.error("订单消息发送失败,未获取到消息类型");
            return;
        }
        if (StringUtils.isEmpty(orderNo)){
            log.error("订单消息发送失败,未获取到订单编号");
            return;
        }
        //获取用户/订单信息
        TdCommodityUserOrder userOrder = tdCommodityUserOrderService.findByOrderNo(orderNo);
        if (ObjectUtils.isEmpty(userOrder)){
            log.error("订单消息发送失败,未获取到订单信息");
            return;
        }
        List<TdCommodityOrderDetails> list = tdCommodityUserOrderService.findOrderDetailByOrderNo(orderNo);
        if (CollectionUtils.isEmpty(list)){
            log.error("订单消息发送失败,未获取到订单商品信息");
            return;
        }

        String openid = userInfoService.findForOpenId(userOrder.getUserInfoId());
        if (StringUtils.isEmpty(openid)){
            log.error("订单消息发送失败,未获取到用户openid");
            return;
        }

        //20分钟未支付订单提醒
        if ("1".equals(typeId)){
            //判断订单是否支付 不是待支付状态直接返回
            if (userOrder.getOrderStatus() != 0){
                log.info("订单消息发送失败,订单已付款");
                return;
            }

            MsgTemplate template = new MsgTemplate();
            template.setTemplate_id(MsgTemplatesEnum.CANCEL_ORDER.gettemplateId());
            template.setTouser(openid);
            HashMap<String, MsgTemplateParam> map = new HashMap<>();
            //拼接订单详情
            String collect = list.stream().map(r -> r.getBrandName()+r.getTypeName()+r.getCommodityName() + r.getCommoditySpecs()).collect(Collectors.joining());
            map.put("thing5",new MsgTemplateParam(MsgTemplatesEnum.CANCEL_ORDER.getDescribe()));
            map.put("character_string4",new MsgTemplateParam(orderNo));
            map.put("thing7",new MsgTemplateParam(collect.substring(0,20)));
            map.put("time8",new MsgTemplateParam(DateUtil.getDate(userOrder.getCreatedAt(),"yyyy年MM月dd日 HH:mm")));
            map.put("amount9",new MsgTemplateParam("￥" + userOrder.getActualAmount().multiply(new BigDecimal(0.01)).setScale(2, RoundingMode.HALF_UP)));
            template.setData(map);
            WechatMsgSendUtil.sendMassage(wechatConfig,template);
        }
    }
}
